<template>
	<view>
		<u-empty v-if="cartGoodsList.length==0" mode="car" icon="http://cdn.uviewui.com/uview/empty/car.png">
		</u-empty>
		<view v-else>
			<OrderGoods v-for="goods in cartGoodsList" :key="goods.goodsId" :goods="goods" class="orderGoods">
			</OrderGoods>
			<view class="userAddress">
				<view class="title">收货地址</view>
				<Address class="address" :item="address" :origin="2"></Address>
			</view>
			<button class="submitBtn" @click="submit">提交订单 ￥{{totalPrice}}</button>
		</view>
		<button class="goShoppingBtn" @click="goShopping()" v-if="cartGoodsList.length==0">前往购物</button>
	</view>
</template>

<script>
	import Address from '@/components/Address.vue'
	import OrderGoods from '@/components/OrderGoods.vue'
	export default {
		components: {
			OrderGoods,
			Address
		},
		data() {
			return {
				address: {},
				cartGoodsList: []
			}
		},
		computed: {
			totalPrice() {
				let sum = 0;
				this.cartGoodsList.forEach(item => {
					sum += item.totalPrice
				})
				return sum;
			}
		},
		onShow() {
			this.cartGoodsList = uni.getStorageSync('cartGoodsList') || [];
			//从收货地址界面回来，获取选中的地址信息
			let pages = getCurrentPages();
			if (pages[pages.length - 1].$vm.address.id) {
				this.address = pages[pages.length - 1].$vm.address
			} else {
				this.getUserDefaultAddress()
			}
		},
		methods: {
			submit() {
				if (!this.address.name) {
					uni.showToast({
						title: '请先设置收货地址',
						icon: 'none'
					})
					return;
				}
				uni.showModal({
					title: '确定要提交订单吗',
					cancelText: '取消',
					confirmText: '确定',
					showCancel: true,
					success: (res) => {
						if (res.confirm) {
							this.addOrder()
						}
					}
				})
			},
			async addOrder() {
				//分解收货地址信息
				const addressInfo = this.address.address.split(' ');
				const res = await this.$myRequest({
					url: 'order/dispatchOrder/submitDispatchOrder',
					method: 'post',
					data: {
						totalPrice: this.totalPrice,
						name: this.address.name,
						phone: this.address.phone,
						province: addressInfo[0],
						city: addressInfo[1],
						region: addressInfo[2],
						detail: addressInfo[3],
						orderGoodsInfoList: this.cartGoodsList
					}
				})
				if (res && res.code === 0) {
					this.cartGoodsList = []
					uni.setStorageSync('cartGoodsList', this.cartGoodsList)
					uni.showModal({
						title: '订单提交成功',
						cancelText: '晚点支付',
						confirmText: '立即支付',
						showCancel: true,
						success: (res) => {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/order/order'
								})
							}
						}
					})
				}
			},
			async getUserDefaultAddress() {
				const res = await this.$myRequest({
					url: 'user/applet/getUserDefaultAddress'
				})
				if (res && res.code === 0) {
					this.address = res.data || {};
				}
			},
			goShopping(){
				uni.navigateTo({
					url:'/pages/shop/shop'
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: 100rpx;
		background-color: #F8F8F8;
	}

	.orderGoods {
		margin-bottom: 10rpx;
	}

	.interval {
		height: 130rpx;
	}

	.userAddress {
		margin: 20rpx 20rpx 120rpx 20rpx;
		background-color: white;
		border-radius: 10px;

		.title {
			font-size: 40rpx;
			color: #FFB300;
			font-weight: 600;
			text-align: center;
		}
	}

	.submitBtn {
		position: fixed;
		bottom: 20rpx;
		background-color: #FFB300;
		color: white;
		width: 400rpx;
		height: 100rpx;
		line-height: 100rpx;
		margin: 0 175rpx;
	}

	.goShoppingBtn {
		position: fixed;
		bottom:500rpx;
		background-color: #FFB300;
		color: white;
		width: 400rpx;
		height: 100rpx;
		line-height: 100rpx;
		margin: 0 175rpx;
	}
</style>
